﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MeiTool.Common
{
    public static class CommonMethod
    {
        /// <summary>
        /// 获取随机数
        /// </summary>
        /// <param name="total">随机数范围从1到total</param>
        /// <param name="need">需要随机数个数</param>
        /// <returns></returns>
        public static int[] GetRandoms(int total, int need)
        {
            if (total <= 0 || need <= 0 || total < need)
            {
                return null;
            }
            Random random = new Random(Guid.NewGuid().GetHashCode());
            int[] a = new int[total];
            int[] result = new int[need];
            int i, m;
            for (i = 1; i <= need; ++i)
            {
                while (a[m = random.Next(0, total)] != 0) ;
                a[m] = i;
                result[i - 1] = m;
            }
            return result;
        }

        /// <summary>
        /// 判断是否存在非法字符串
        /// </summary>
        /// <param name="source">字符串</param>
        /// <param name="space">true 判断空格</param>
        /// <returns></returns>
        public static bool CheckCharacter(string source, bool space = false)
        {
            if (string.IsNullOrEmpty(source)) return false;

            var result = false;
            string regExp = "~!@#$%^&*()=+[]\\{}'‘’\";:/?.,><`|！·￥…—（）-、；：。，》《【】”“、？" + (space ? " " : "");
            char[] charstr = regExp.ToCharArray();
            foreach (var item in charstr)
            {
                if (source.Contains(item))
                {
                    result = true;
                    break;
                }
            }
            return result;
        }

        /// <summary>
        /// 比较两个日期的天数查
        /// </summary>
        /// <param name="dateStart"></param>
        /// <param name="dateEnd"></param>
        /// <returns></returns>
        public static int DateDiff(DateTime dateStart, DateTime dateEnd)
        {
            DateTime start = Convert.ToDateTime(dateStart.ToShortDateString());

            DateTime end = Convert.ToDateTime(dateEnd.ToShortDateString());

            TimeSpan sp = end.Subtract(start);

            return Math.Abs(sp.Days); 
        }

        

    }
}
